package com.example.firstjavamqtt.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //默认
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("第二个物联网项目")
                        .description("这里煤球")
                        .version("9.0")
                        .contact(new Contact("我的gitee主页", "https://gitee.com/jason0131czy", "不告诉你@qq.com"))
                        .build());
    }

    //日志界面
    @Bean
    public Docket logApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("logApi")
                .apiInfo(logApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/log/.*")))
                .build();
    }

    //与硬件交互的mqtt界面
    @Bean
    public Docket mqttApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("mqttApi")
                .apiInfo(mqttApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/mqtt/.*")))
                .build();
    }

    //基础api
    @Bean
    public Docket baseApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("baseApi")
                .apiInfo(baseApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/base/.*")))
                .build();
    }


    private ApiInfo baseApiInfo() {
        return new ApiInfoBuilder()
                .title("物联网项目-基础API文档")
                .description("本文档描述了物联网项目的后台基础接口API")
                .version("1.0")
                .contact(new Contact("我的gitee主页", "https://gitee.com/jason0131czy", "不告诉你@@qq.com"))
                .build();
    }

    private ApiInfo logApiInfo() {
        return new ApiInfoBuilder()
                .title("物联网项目-日志API文档")
                .description("本文档描述了物联网项目的后台日志接口API")
                .version("1.0")
                .contact(new Contact("我的gitee主页", "https://gitee.com/jason0131czy", "不告诉你@@qq.com"))
                .build();
    }

    private ApiInfo mqttApiInfo() {
        return new ApiInfoBuilder()
                .title("物联网项目-MQTT-API文档")
                .description("本文档描述了物联网项目的硬件交互MQTT层文档")
                .version("1.0")
                .contact(new Contact("我的gitee主页", "https://gitee.com/jason0131czy", "不告诉你@@qq.com"))
                .build();
    }
}

